# -*- coding: utf-8 -*-

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("QDateTimeEdit示例")
        self.resize(300, 90)

        vlayout = QVBoxLayout()
        self.dateEdit = QDateTimeEdit(QDateTime.currentDateTime(), self)
        self.dateEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
        self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-365))
        self.dateEdit.setMaximumDate(QDate.currentDate().addDays(365))
        self.dateEdit.setCalendarPopup(True)

        self.dateEdit.dateChanged.connect(self.on_date_changed)
        self.dateEdit.dateTimeChanged.connect(self.on_date_time_changed)
        self.dateEdit.timeChanged.connect(self.on_time_changed)

        self.btn = QPushButton("获取日期和时间")
        self.btn.clicked.connect(self.on_button_click)

        vlayout.addWidget(self.dateEdit)
        vlayout.addWidget(self.btn)
        self.setLayout(vlayout)

    def on_date_changed(self, date):
        print(date)

    def on_date_time_changed(self, dateTime):
        print(dateTime)

    def on_time_changed(self, time):
        print(time)

    def on_button_click(self):
        dateTime = self.dateEdit.dateTime()
        maxDate = self.dateEdit.maximumDate()
        maxDateTime = self.dateEdit.maximumDateTime()
        maxTime = self.dateEdit.maximumTime()
        minDate = self.dateEdit.minimumDate()
        minDateTime = self.dateEdit.minimumDateTime()
        minTime = self.dateEdit.minimumTime()

        print("选择日期时间\ndateTime=%s\nmaxDate=%s\nmaxDateTime=%s\nmaxTime=%s\nminDate=%s\nminDateTime=%s\nminTime=%s\n" %
              (str(dateTime), str(maxDate), str(maxDateTime), str(maxTime), str(minDate), str(minDateTime), str(minTime)))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec())